require("widget/BaseUI")
WaitingBox = {}
WaitingBox.m_window = nil

function WaitingBox:destroyInstance()
  _G.WaitingBox = false
end

function WaitingBox:getInstance()
  local o = _G.WaitingBox
  if o then
    return o
  end
  o = {}
  _G.WaitingBox = o
  setmetatable(o, self)
  self.__index = self
  return o
end

function WaitingBox:open(content, isshowBg)
  cc.SpriteFrameCache:getInstance():addSpriteFrames("ui/common.1.plist")
  if not WaitingBox:getInstance().m_window then
    local data = getWndInitParamList()
    data.isShowClose = false
    data.isShowCover = true
    data.isSwollowTouch = true
    data.isShowImage = false
    data.isRemoveResource = false
    data.opensoundPath = ""
    WaitingBox:getInstance().m_window = require("widget/BaseUI").new()
    WaitingBox:getInstance().m_window.onEnterForSubClass = function()
      WaitingBox:getInstance().m_window:showWaitingForService(content)
      self:cancleEntry()
      self.m_closeEntry = cc.Director:getInstance():getScheduler():scheduleScriptFunc(handler(self, self.loadingTimeout), 5, false)
    end
    WaitingBox:getInstance().m_window.onExitForSubClass = function()
      self:cancleEntry()
      WaitingBox:getInstance().m_window = nil
    end
    WaitingBox:getInstance().m_window:initWithParas(data)
    WaitingBox:getInstance().m_window:setBaseWindowSize(WinSize.width, WinSize.height)
    WaitingBox:getInstance().m_window:openWindow()
    WaitingBox:getInstance().m_window:setLocalZOrder(math.pow(2, 32))
  end
end

function WaitingBox:setLoadingTimeoutTime(dt)
  self:cancleEntry()
  self.m_closeEntry = cc.Director:getInstance():getScheduler():scheduleScriptFunc(handler(self, self.loadingTimeout), dt, false)
end

function WaitingBox:loadingTimeout(dt)
  LabelTipsOneByOneManager:getInstance():addTips({
    str = LocalizedString("WaitingBox_load_timeout"),
    time = 4
  })
  self:onCloseTips()
end

function WaitingBox:cancleLoadingTimeout()
  self:cancleEntry()
end

function WaitingBox:onCloseTips()
  self:cancleEntry()
  if WaitingBox:getInstance().m_window ~= nil then
    WaitingBox:getInstance().m_window:removeWindow()
    WaitingBox:getInstance().m_window = nil
  end
end

function WaitingBox:cancleEntry(dt)
  if self.m_closeEntry then
    cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.m_closeEntry)
    self.m_closeEntry = nil
  end
end
